package com.compty.paint.web.member;

import java.io.File;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.compty.core.struts2.BaseAction;
import com.compty.core.utils.CommonsUtil;
import com.compty.paint.domain.BackgroundImage;
import com.compty.paint.domain.CatalogType;
import com.compty.paint.domain.ImageInfo;
import com.compty.paint.domain.SharpInfo;
import com.compty.paint.manager.BackgroundImageManager;
import com.compty.paint.manager.CatalogTypeManager;
import com.compty.paint.manager.ImageInfoManager;
import com.compty.paint.manager.SharpInfoManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * 摆设背景图片管理
 * @author: 朱庆辉
 * @CreateTime:2011-7-12 下午03:58:26
 */
public class BackImageAction   extends BaseAction<BackgroundImage>{
	
	private static final long serialVersionUID = 9194412494830903787L;
	
	private BackgroundImageManager backgroundImageManager;
	private SharpInfoManager sharpInfoManager;
	private CatalogTypeManager catalogTypeManager;
	private ImageInfoManager imageInfoManager;

	private BackgroundImage backgroundImage;

	/**
	 * 添加背景图片
	 *@CreateTime:2011-7-12 下午04:56:36
	 */
	public String addPrepare(){
		
		this.backgroundImage = null;
		List<SharpInfo> sharpInfoList = sharpInfoManager.getAll();
		request.setAttribute("sharpInfoList", sharpInfoList);
		List<CatalogType> catalogTypeList = catalogTypeManager.getAll();
		request.setAttribute("catalogTypeList", catalogTypeList);
		return "addPrepare";
	}
	
	/**
	 * 显示摆设列表
	 *@author:朱庆辉
	 *@CreateTime:2011-7-12 下午04:56:23
	 */
	public String list(){
		
	  Criteria criteria;
        if (sort != null) {
            boolean isAsc = dir.equalsIgnoreCase("asc");
            criteria = backgroundImageManager.createCriteria(sort, isAsc);
        } else {
            criteria = backgroundImageManager.createCriteria();
        }
        
        criteria.addOrder(Order.desc("id"));
        
        User userInfo = (User) session.getAttribute(Constants.Userinfo);
        criteria = criteria.add(Restrictions.eq("entryPerson", userInfo));
        

        if ((filterTxt != null) && (filterValue != null)&& (!filterTxt.equals("")) && (!filterValue.equals(""))) {
            criteria = criteria.add(Restrictions.like(filterTxt,"%" + filterValue + "%"));
        }

        this.page = backgroundImageManager.pagedQuery(criteria, curPage, pageSize);
		
		return "list";
	}
	
	/**
	 * 保存摆设图
	 *@author:zhuqh
	 *@CreateTime:2011-7-12 下午05:55:29
	 */
	public String save(){
		if(backgroundImage != null){
			String picImage = request.getParameter("picImage");
			
			User userInfo = (User) session.getAttribute(Constants.Userinfo);
			backgroundImage.setEntryPerson(userInfo);
			backgroundImage.setModifyTime(Calendar.getInstance().getTime());
			backgroundImage.setUploadTime(Calendar.getInstance().getTime());
			
			String imageFilePath = getImageUploadPath()+CommonsUtil.getPathSymbol() + picImage; //图片物理路径
			File file = new File(imageFilePath);
			ImageInfo imageInfo = saveImageToDB(file, picImage, "8",picImage,Constants.BackgroundFlag);
			backgroundImage.setImageInfo(imageInfo);
			
			backgroundImageManager.save(backgroundImage);
		}
		
		return "saved";
	}
	
	/**
	 *@Todo:详情
	 *@CreateTime:2011-6-9 上午09:46:18
	 */
	public String modifyPrepare(){
		
		if(backgroundImage != null){
			
			Long id = backgroundImage.getId();
			this.backgroundImage = backgroundImageManager.get(id);
		}
		
		return "modifyPrepare";
	}
	
	/**
	 * 修改展览信息
	 *@CreateTime:2011-6-9 上午10:19:01
	 * @return
	 */
	public String modify(){
		
		if(backgroundImage != null){
			
			BackgroundImage bg = backgroundImageManager.get(backgroundImage.getId());
			if(backgroundImage.getImageInfo() != null && backgroundImage.getImageInfo().getId() == null){
				
				String picImage = request.getParameter("backgroundImage.imageInfo.id");
				String imageFilePath = getImageUploadPath()+CommonsUtil.getPathSymbol() + picImage; //图片物理路径
				File file = new File(imageFilePath);
				ImageInfo imageInfo = saveImageToDB(file, picImage, "8",picImage,Constants.BackgroundFlag);
				bg.setImageInfo(imageInfo);
			}else{
				bg.setImageInfo(backgroundImage.getImageInfo());
			}
			
			bg.setWidth(backgroundImage.getWidth());
			bg.setHeight(backgroundImage.getHeight());
			bg.setTitle(backgroundImage.getTitle());
			
			bg.setSize_horizontal(backgroundImage.getSize_horizontal());
			bg.setSize_vertical(backgroundImage.getSize_vertical());
			bg.setSize_square(backgroundImage.getSize_square());
			
			bg.setPosition(backgroundImage.getPosition());
			
			bg.setModifyTime(Calendar.getInstance().getTime());
		
			backgroundImageManager.save(bg);
		}
		
		request.setAttribute("ajaxData", "0000");
		
		return "modified";
	}
	
	/**
	 * 删除 
	 *@CreateTime:2011-7-13 下午04:47:14
	 */
	public String delete(){
		
		if(backgroundImage != null){
			
			Long id = backgroundImage.getId();
			backgroundImageManager.removeById(id);
		}

		return "deleted";
	}
	
	/**
	 *@Todo:保存图片至DB
	 * @param file 图片文件
	 * @param originalName  图片原始名字
	 * @param sharpId 图片形状类型
	 */
	public ImageInfo saveImageToDB(File file,String originalName,String sharpId,String newNameUploaded,String memo){
		ImageInfo imageInfo = buildImage(file,originalName,sharpId,newNameUploaded,memo);
    	imageInfoManager.save(imageInfo);
    	writeImg(imageInfo,Constants.BackgroundFlag);
    	return imageInfo;
	}

	public BackgroundImage getBackgroundImage() {
		return backgroundImage;
	}

	public void setBackgroundImage(BackgroundImage backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

	public void setBackgroundImageManager(
			BackgroundImageManager backgroundImageManager) {
		this.backgroundImageManager = backgroundImageManager;
	}
	
	public void setSharpInfoManager(SharpInfoManager sharpInfoManager) {
		this.sharpInfoManager = sharpInfoManager;
	}

	public void setImageInfoManager(ImageInfoManager imageInfoManager) {
		this.imageInfoManager = imageInfoManager;
	}

	public void setCatalogTypeManager(CatalogTypeManager catalogTypeManager) {
		this.catalogTypeManager = catalogTypeManager;
	}
	
}
